<?php
$path = dirname(__FILE__) . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "..";
if(!empty($_POST['addTheme']) && $_POST['addTheme'] == "1"){
	
	$n = $_POST['name'];
	$d = array("themes",$n,"views","layouts");
	$p = "";
	foreach($d as $k=>$v){
		$p.= "/".$v;
		@mkdir($path.$p, 0777);
	}
	
}

function get_dirs($dir){ 
    $dirs = array(); 
    if (!isset($dirs)){$dirs = '';} 
    if(substr($dir,-1) !== '\\'){$dir .= '\\';} 
	
    if ($handle = opendir($dir)){ 
		
        while (false !== ($file = readdir($handle))){ 
			if ($file != "." && $file != "..") $dirs[] = $file;
        } 
        closedir($handle); 
    } 
    return $dirs; 
}
function find_all_files($dir) 
{ 
	$root = scandir($dir); 
	$result = array();
	foreach($root as $value) 
	{ 
		if($value === '.' || $value === '..') {continue;} 
		if(is_file("$dir/$value")) {$result[]="$value";continue;} 
		foreach(find_all_files("$dir/$value") as $value) 
		{ 
			$result[]=$value; 
		} 
	} 
	return $result; 
}
$_template  = dirname(__FILE__) . DIRECTORY_SEPARATOR . ".." . "/templates/default/yii";
$fileTemplate = find_all_files($_template);
$results = get_dirs($path);

$folderTheme = array();
if(empty($results)){
	
}else{
	if (in_array("themes", $results)) {
		$folderTheme = get_dirs($path."/themes");
	}	
}
?>

	
<div style="width:100%; text-align:right">	
	<?php if(empty($folderTheme)): ?>
		<form  method="POST" action="">
			<input type="hidden" name="addTheme" value="1">
			<input type="text" name="name">
			<input type="submit" value="Add Theme">
			
		</form>
	<?php else:?>
	<select id='item_layout'>
		<?php
		echo "<option value='0'>...</option>";
		foreach($folderTheme as $key=>$name):
			echo "<option value='{$name}'>{$name}</option>";
		endforeach;
		?>
	</select>
	<span id="nav1" style="display:none">
		<a id="theme_setting" href="#data">Config</a>
	</span>
	<span id="nav2" style="display:inline-block">
		<a  href="javascript:void(0)">Config</a>
	</span>
	
	<?php endif;?>
</div>


<div style="display:none">
	<div id="data">
		<div style="width:800px">
			<div>
				Layout: <input type="text" name="name"> 
				<button>
					Use
				</button>
			</div>
			<div>
				<div style="width:200px;float:left">
					<?php
					echo "<ul>";
					foreach($fileTemplate as $key=>$file){
						echo "<li><a href='#'>{$file}</a></li>";
					}
					echo "</ul>";
					?>
				</div>
				<div style="width:599px; float:left">
				a
				</div>
			</div>
			
		</div>
	</div>
</div>
<script>
$(document).ready(function() {
	$("#item_layout").live("change",function(e){
		console.log($(this).val());
		if($(this).val() == 0){
			$("#nav2").show();
			$("#nav1").hide();
		}else{
			$("#nav2").hide();
			$("#nav1").show();
		}
	});
	$("a#theme_setting").fancybox({
		'hideOnContentClick': true
	});

	
	
	
});
</script>
<?php



$class=get_class($model);
Yii::app()->clientScript->registerScript('gii.crud',"
$('#{$class}_controller').change(function(){
	$(this).data('changed',$(this).val()!='');
});
$('#{$class}_model').bind('keyup change', function(){
	var controller=$('#{$class}_controller');
	if(!controller.data('changed')) {
		var id=new String($(this).val().match(/\\w*$/));
		if(id.length>0)
			id=id.substring(0,1).toLowerCase()+id.substring(1);
		controller.val(id);
	}
});
");
?>
<h1>Crud Generator</h1>

<p>This generator generates a controller and views that implement CRUD operations for the specified data model.</p>

<?php $form=$this->beginWidget('CCodeForm', array('model'=>$model)); ?>

	<div class="row">
		<?php echo $form->labelEx($model,'model'); ?>
		<?php echo $form->textField($model,'model',array('size'=>65)); ?>
		<div class="tooltip">
			Model class is case-sensitive. It can be either a class name (e.g. <code>Post</code>)
		    or the path alias of the class file (e.g. <code>application.models.Post</code>).
		    Note that if the former, the class must be auto-loadable.
		</div>
		<?php echo $form->error($model,'model'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'controller'); ?>
		<?php echo $form->textField($model,'controller',array('size'=>65)); ?>
		<div class="tooltip">
			Controller ID is case-sensitive. CRUD controllers are often named after
			the model class name that they are dealing with. Below are some examples:
			<ul>
				<li><code>post</code> generates <code>PostController.php</code></li>
				<li><code>postTag</code> generates <code>PostTagController.php</code></li>
				<li><code>admin/user</code> generates <code>admin/UserController.php</code>.
					If the application has an <code>admin</code> module enabled,
					it will generate <code>UserController</code> (and other CRUD code)
					within the module instead.
				</li>
			</ul>
		</div>
		<?php echo $form->error($model,'controller'); ?>
	</div>

	<div class="row sticky">
		<?php echo $form->labelEx($model,'baseControllerClass'); ?>
		<?php echo $form->textField($model,'baseControllerClass',array('size'=>65)); ?>
		<div class="tooltip">
			This is the class that the new CRUD controller class will extend from.
			Please make sure the class exists and can be autoloaded.
		</div>
		<?php echo $form->error($model,'baseControllerClass'); ?>
	</div>

<?php $this->endWidget(); ?>
